<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Services\ItemBankService;
use App\Services\EvaluationService;
use App\Services\CourseService;
use App\Services\BabyService;
use App\Services\MemberService;
use App\Services\TagService;
use App\Services\TestReportService;
use Session;

class EvaluationController extends Controller{
    public function evaluationlog($mid){
        $tag = new TagService;
        $baby = new BabyService;
        $evaluation = new EvaluationService;

        $list = $baby->getBabyList($mid);
        if(empty($list)){
            $_data = array('code'=>'4x000001','msg'=>'暂无信息');
        }else{
            foreach($list as $itm=>$lst){
                $list[$itm]['assess_log_list'] = $evaluation->getAssessList($lst['id']);
            }
            $_data = array('code'=>'2x000001','list'=>$list);
        }
        return response()->json($_data);
    }
	public function qlist($mid,$bid){

        $member = new MemberService;
        $userinfo = $member->getMemberInfoByid($mid);
        if($bid == 'null'){
            $_data = array('code'=>'3x000002','msg'=>'请添加宝宝');
        }else{
            $_data['code']='2x000001';
            $item = new ItemBankService;
            $_data['memberid'] = $mid;
            $_data['babyid'] = $bid;

            $questionNum = $item->getQuestionNum();
            $questionlist = $item->getQuestionList($questionNum,'','asc');

            $_data['userinfo'] = $userinfo;
            $_data['questionNum'] = $questionNum;
            $_data['questionlist'] = $questionlist;
        }
		return response()->json($_data);
	}

	public function qadd(){

        date_default_timezone_set('PRC');
        $evaluation = new EvaluationService;

        $eval_list = $_POST['eval_list'];

        foreach($eval_list as $key=>$val){
            if(strpos($val,',') !== FALSE){
                $eval_list[$key] = explode(',',$val);
            }else{
                $eval_list[$key] = array($val);
            }
        }


        foreach($eval_list as $etm=>$est){
            foreach($est as $et){
                $data['member_id'] = intval($_POST['member_id']);
                $data['baby_id'] = intval($_POST['baby_id']);
                $data['question_id'] = $etm;
                $data['option_id'] = $et;
                $data['score'] = 1;
                $data['add_date'] = date('Y-m-d H:i:s',time());

                $res = $evaluation->insertEvaluation($data);
            }
        }

        if($res){
            $log_data['member_id'] = intval($_POST['member_id']);
            $log_data['baby_id'] = intval($_POST['baby_id']);
            $log_data['assess_date'] = date('Y-m-d');

            $evaluation->insertAssess($log_data);

            $_data = array('code'=>'2x000001','msg'=>'测评成功');
        }else{
            $_data = array('code'=>'4x000001','msg'=>'测评失败');
        }

		return response()->json($_data);
	}

	public function getEvaluationRes($mid,$bid){
		if(!$mid){
			$_data = array('code'=>'3x000001','msg'=>'请登录','url'=>env('APP_URL'));
		}else{
			$evaluation = new EvaluationService;
			$baby = new BabyService;
			$tag = new TagService;
            $testreport = new TestReportService;

			$babyinfo = $baby->getBabyInfo($bid);
			$evallist = $evaluation->getEvaluation($mid,$bid);


			$tag_list = array();
			foreach($evallist as $etm=>$est){
			    if(!empty($est['score_list'])){
                    foreach($est['score_list'] as $esm=>$smt){
                        $tag_list[] = $esm;
                    }
                }
            }
            $tag_list = array_unique($tag_list);

			$tag_name_list = array();
			foreach($tag_list as $tlm=>$tlt){
			    $taginfo = $tag->getTagInfoById($tlt);
			    $tag_name_list[] = $taginfo->tag_name;
            }

			$course = new CourseService;
			// $courseNum = $course->getCourseNum($tag_list);
			$courserecommend = $course->getCourseListByTag($tag_list);

			$testreport_list = $testreport->getReportList();

			$_data['evallist'] = $evallist;
			$_data['courserecommend_list'] = $courserecommend;
			$_data['babyinfo'] = $babyinfo;
			$_data['tag_name_list'] = $tag_name_list;
			$_data['test_report_list'] = $testreport_list;
		}
		return response()->json($_data); 
	}
}